package com.lanyun.util;


public class DistanceUtil {

    private static final double EARTH_RADIUS = 6378137;
    private static final double RAD = Math.PI / 180.0;

    /**
     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米
     *
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     * @return
     */
    public static int getDistance(Double lng1, Double lat1, Double lng2, Double lat2) {
        Double radLat1 = lat1 * RAD;
        Double radLat2 = lat2 * RAD;
        Double a = radLat1 - radLat2;
        Double b = (lng1 - lng2) * RAD;
        Double s = 2 * Math.asin(Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000.0;
        return (int)Math.round(s);
    }
    public static int getDistance(String s1, String s2) {
        String[] str1 = s1.split(",");
        double lat1 = Double.parseDouble(str1[1]);
        double lng1 = Double.parseDouble(str1[0]);
        String[] str2 = s2.split(",");
        double lat2 = Double.parseDouble(str2[1]);
        double lng2 = Double.parseDouble(str2[0]);
        double radLat1 = lat1 * RAD;
        double radLat2 = lat2 * RAD;
        double a = radLat1 - radLat2;
        Double b = (lng1 - lng2) * RAD;
        Double s = 2 * Math.asin(Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000.0;
        return (int)Math.round(s);
    }
}